package com.qianchang.microfinance.view.base;


import com.qianchang.microfinance.bean.GtDate;
import com.qianchang.microfinance.bean.GtDateType;
import com.qianchang.microfinance.bean.PolicySet;
import com.qianchang.microfinance.dao.GtDateDao;
import com.qianchang.microfinance.dao.PolicySetDao;
import com.qianchang.microfinance.utils.DMConstant;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMLog;
import com.dm.library.utils.DateUtil;
import com.dm.library.utils.JsonUtils;
import com.dm.library.utils.StringUtils;
import com.dm.library.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * 修 改 人:  jiaohongyun
 * 修改时间:  2016/8/24
 */
public class InitConstantBaseActivity extends BaseActivity {


    private static final String TAG = "InitConstantApi";

    public void getSocialAccountType(final InitCallBack callBack) {
        show();
        final GtDate gtDate = GtDateDao.getInstance().getGtDateByType(InitConstantBaseActivity.this, GtDateType.POLICYSET.name());
        HttpParams params = new HttpParams();
        params.put("configType", "SYSTEM");
        params.put("gtDate", (gtDate == null) ? DateUtil.millis2String(System.currentTimeMillis(), new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.getDefault())) : gtDate.getGtDate());
        DMOKHttpUtils.getInstance().post(InitConstantBaseActivity.this, DMConstant.ApiURL.GET_CONFIG, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(JsonResult<String> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    DMLog.d(TAG, response.getObject());
                    try {
                        JSONObject dataObj = new JSONObject(response.getObject());
                        // 没有做更改的情况下，{"data":{"policySets":[]},"code":"000000"}
                        String updateTime = JsonUtils.getString(dataObj, "updateTime");
                        if (!StringUtils.isEmptyOrNull(updateTime)) {
                            GtDateDao.getInstance().save(InitConstantBaseActivity.this, new GtDate(GtDateType.POLICYSET.name(), updateTime));
                        }

                        List<PolicySet> policySetList =
                                new Gson().fromJson(JsonUtils.getString(dataObj, "policySets"), new TypeToken<List<PolicySet>>() {
                                }.getType());

                        if (gtDate == null && (PolicySetDao.getInstance().count(InitConstantBaseActivity.this) == 0)) {
                            PolicySetDao.getInstance().saveAll(InitConstantBaseActivity.this, policySetList);
                        } else {
                            PolicySetDao.getInstance().replaceAll(InitConstantBaseActivity.this, policySetList);
                        }
                        callBack.success();
                    } catch (JSONException e) {
                        e.printStackTrace();
//                        ToastUtil.getInstant().show(InitConstantBaseActivity.this, getString(R.string.data_error));
                        callBack.fail();
                    }
                } else {
                    callBack.fail();
//                    ToastUtil.getInstant().show(InitConstantBaseActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(InitConstantBaseActivity.this, msg);
                callBack.fail();
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }



    public interface InitCallBack {
        void success();

        void fail();
    }

}
